package com.sc.nft.wrapper;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
import java.util.List;

public class RequestWrapper extends HttpServletRequestWrapper {
    private final String body;
    private final List<String> filterUrlList = Lists.newArrayList(
            "/admin/oss/uploadFile",
            "/admin/AirdropManagement/upLoadAirdropUserExcel",
            "/admin/product/addBuyFirstUserExcel",
            "/admin/user/userInformationExport",
            "/admin/order/orderInformationExport",
            "/pay/cloudAccount/protocol/notify",
            "/pay/cloudAccount/pwd/notify",
            "/pay/cloudAccount/withdraw/notify",
            "/pay/cloudAccount/cancel/notify",
            "/pay/cloudAccount/upload",
            "/admin/mall/order/batchDeliver",
            "/admin/userAssets/batchEnterIntegrationShow",
            "/admin/userAssets/analyze",
            "/admin/pushManage/uploadTagUserExcel",
            "/admin/ecologyPool/excellentBrandUpload",
            "/admin/ecology/smelting/userRecordsExport",
            "/user/faceVerificationCallBack",
            "/admin/userConsensus/excelPassing"
    );
    public RequestWrapper(HttpServletRequest request) {
        super(request);
        String url = request.getRequestURI();
        if (filterUrlList.contains(url) ){
            body = null;
            return;
        }
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        InputStream inputStream = null;
        try {
            inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuilder.append("");
            }
//            if(StrUtil.isBlank(stringBuilder)){
//                stringBuilder.append(JSONObject.toJSONString(request.getParameterMap()));
//            }
        } catch (IOException ex) {

        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        body = stringBuilder.toString();
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
        ServletInputStream servletInputStream = new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }
            @Override
            public boolean isReady() {
                return false;
            }
            @Override
            public void setReadListener(ReadListener readListener) {
            }
            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }
        };
        return servletInputStream;

    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(this.getInputStream()));
    }

    public String getBody() {
        return this.body;
    }

}